<?php

define('REQUEST_PAGE', 'page');
define('REQUEST_SEARCH', 'search');

function resolve_template_name($config, $request) {
	if (isset($request['template']) && $request['template'] != '') {
		return $request['template'];
	}
	return $config['template'];
}

function resolve_request_type($get) {
	if (isset($get['query']) && $get['query'] != '') {
		return REQUEST_SEARCH;
	}
	return REQUEST_PAGE;
}

function db_get_page($name) {
	global $db, $pages_table, $authors_table;
	
	if ($db->count($pages_table, 'id', "name = '" . $name . "'") == 1) {
		$result = $db->get_result("SELECT $pages_table.*, $authors_table.first_name, $authors_table.last_name 
			FROM $pages_table, $authors_table
			WHERE $authors_table.id = $pages_table.author_id AND $pages_table.name = '$name' 
			AND $pages_table.viewed = true");
		if ($result != null) {
			return $result[0];
		}
	}
	
	return null;
}

function db_search_pages($query) {
	global $db, $pages_table, $authors_table;
	
	return $db->get_result("SELECT id,name,title,`text`,viewed 
		FROM $pages_table WHERE viewed = 1 AND (title LIKE '%$query%' OR `text` LIKE '%$query%')");	
}

function db_get_menu_pages() {
	global $db, $pages_table;
	
	return $db->get_result("SELECT id, title, name FROM $pages_table
		WHERE viewed = true ORDER BY `index` ASC");
}

function debug($str) {
	if (DEBUG) {
		echo 'DEBUG: ' . $str . '<br/>';
	}
}

function error_uri($name) {
	return 'error/' . $name . '.html';
}

function redirect($location) {
	header('Location: '.$location);
	die();
}

function set_flash($message, $page) {
	$_SESSION['flash'] = $message;
	redirect($page);
}

function get_flash() {
	if (isset($_SESSION['flash'])) {
		$msg = $_SESSION['flash'];
		unset($_SESSION['flash']);
		return $msg;
	}
	return null;
}

function escape($str) {
	return mysql_real_escape_string($str);
}

function escape_array($arr) {
	$res = array();
	foreach ($arr as $key => $value) {
		$res[$key] = mysql_real_escape_string($value);
	}
	return $res;
}

function kc_die($message = "")
{
	?>
	<html>
		<head>
			<title>KomitCMS | FEJL</title>
			<link rel="stylesheet" type="text/css" href="stylesheet/style.css" />
			<meta http-equiv="content-type" content="text/html;charset=utf-8" />
			<meta http-equiv="Content-Style-Type" content="text/css" />
		</head>
		<body>
			<h2>Der er opstået en fejl! (<? echo $_SERVER['PHP_SELF']?>)</h2>
			<? 
			if ($message != "")
			{
				?><h3>Problem: <? echo $message ?></h3><br/><?
			}
			?>
			</body>
	</html>
	<?php
	die();
}

function is_rewrite_enabled() 
{
	if (function_exists('apache_get_modules')) {
		return in_array("mod_rewrite", apache_get_modules());
	}
	return false;
}

function use_mod_rewrite()
{
	return (USE_MOD_REWRITE && is_rewrite_enabled());
}

function page_link($name)
{
	if (USE_MOD_REWRITE && is_rewrite_enabled()) 
	{
		return $name . ".html";
	} 
	return "index.php?name=" . $name;
}

function get_value($array, $key) {
	if (isset($array[$key])) {
		return $array[$key];
	}
	return '';
}
function truncate($string, $limit, $break=".", $pad="...")
{
  // return with no change if string is shorter than $limit
  if(strlen($string) <= $limit) return $string;

  // is $break present between $limit and the end of the string?
  if(false !== ($breakpoint = strpos($string, $break, $limit))) {
    if($breakpoint < strlen($string) - 1) {
      $string = substr($string, 0, $breakpoint) . $pad;
    }
  }
    
  return $string;
}


?>
